package com.lian.aieduc.controller;

import com.lian.aieduc.model.dto.*;
import com.lian.aieduc.model.vo.UserVO;
import com.lian.aieduc.service.UsersService;
import com.lian.aieduc.utils.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/users")
@RequiredArgsConstructor
@Tag(name = "用户相关接口", description = "用户相关API接口")
public class UsersController {
    private final UsersService usersService;

    @Operation(summary = "发送短信")
    @PostMapping("/send/sms")
    @Parameter(description = "发送短信需要的信息")
    public Result<String> sendSms(@RequestBody SendSmsDTO sendSmsDTO) {
       return usersService.sendSMS(sendSmsDTO);
    }

    @PostMapping("/sign-in/sms")
    @Operation(summary = "手机短信验证码登录注册")
    public Result<String> signInSms(@RequestBody SmsSignInDTO smsSignInDTO) {
        // 用户存在直接生成token返回用户信息
        return usersService.signInSms(smsSignInDTO);
    }

    @Operation(summary = "发送邮箱验证码")
    @PostMapping("/send/email-code")
    public Result<String> sendEmailCode(@RequestBody SendMailCodeDTO sendMailCodeDTO) throws MessagingException {
        log.info(sendMailCodeDTO.getEmail());
        return usersService.sendEmailCode(sendMailCodeDTO);
    }

    @Operation(summary = "邮箱验证码登录注册")
    @PostMapping("/sign-in/email")
    public Result<String> signInEmail(@RequestBody SignUpEmailDTO signUpEmailDTO) {
        return   usersService.signInEmail(signUpEmailDTO);
    }


    @Operation(summary = "获取当前登录用户信息")
    @Parameter(description = "当前登录用户token",required = true)
    @GetMapping
    public Result<UserVO> getUserById(@RequestHeader String Authorization) {
        log.info("authorization:{}",Authorization);
        if(Authorization == null)return  Result.failure("请传递请求头");
      Long userId =   usersService.pareTokenGetUserId(Authorization);
       return usersService.getUserById(userId);
    }


    @Operation(summary = "更新当前登录用户信息")
    @Parameter(description = "当前登录用户的token",required = true)
    @PutMapping
    public Result<UserVO> updateUserInfoById(@RequestHeader String Authorization,@RequestBody UserInfoDTO userInfoDTO) {

log.info("userInfoDto:{}",userInfoDTO);
        if(Authorization == null)return  Result.failure("请传递请求头");
        Long userId = usersService.pareTokenGetUserId(Authorization);

        if(userId == null)
            return Result.failure("token校验失败请重新登录");
        return usersService.updateUserInfoById(userId,userInfoDTO);
    }



}
